//
//  HomeTableViewController.swift
//  Sina
//
//  Created by 王杰 on 15/11/8.
//  Copyright © 2015年 王杰. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD
private let HomeCellId = "HomeCellId"

class HomeTableViewController: BaseTableViewController {

    private lazy var statuses = [Status]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !userLogin {
            
            visitorLoginView?.setUIInfo(nil, title: "关注一些人，回这里看看有什么惊喜")
            return
        }
        //准备tableView
        prepareTableView()
  
        loadData()
    }
    private func prepareTableView() {
        //菊花按钮的显示
        refreshControl = UIRefreshControl()
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        //1.注册cell
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: HomeCellId)
        tableView.separatorStyle = .None
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.tableFooterView = indicatorView
    }
    
    //MARK: 加载微博首页数据
   @objc private func loadData() {
    
        var since_id = 0
        var max_id = 0
    if  indicatorView.isAnimating() {
    
        since_id = 0
        max_id = statuses.last?.id ?? 0
    }else {
    
        max_id = 0
        
        since_id = statuses.first?.id ?? 0
    }
    
        StatusListViewModel.loadData(since_id: since_id,max_id: max_id) { (array) -> () in
           print(array?.count)
            //刷新数据
            self.refreshControl?.endRefreshing()
            
            if array == nil {

                //请求失败
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                self.indicatorView.stopAnimating()
                return
            }
            
            if since_id > 0 {
            
            self.statuses = array! + self.statuses
                
            }else if max_id > 0 {
            
            self.statuses += array!
               
                self.indicatorView.stopAnimating()
            
            } else {
            
                self.statuses = array!
            }
            //刷新表格
            self.tableView.reloadData()
        }
   
    }
    
    private lazy var indicatorView: UIActivityIndicatorView = {
    
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        return indicator
    }()

}
//定义extension  包含了所有的数据源方法 和协议方法
extension HomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! StatusCell
        let status = statuses[indexPath.row]
        cell.status = status
        if indexPath.row == statuses.count - 1 && !indicatorView.isAnimating() {
            
            indicatorView.startAnimating()
            loadData()
        }
        return cell
    }
}





























